
import os
import sys
import json

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

sys.path.append(os.path.dirname(__file__))
import analysis

context = None
def init(context_):
    global context
    context = context_

class ActionNewNode:
    def __init__(self, kind, node):
        self._kind = kind
        self._node = node
    def __del__(self):
        pass
    def on_analysis(self, value):
        global context
        # 遍历项目中所有File，对每一个File进行源码分析
        files = []

        nodes = []
        nodes.append(self._node)

        while len(nodes)>0:
            n = nodes.pop()
            if n.get_kind() == 'File':
                files.append(n)
            else:
                ns = n.get_children()
                for nnode in ns:
                    nodes.append(nnode)

        data_service = context.find_service("DataService")
        for f in files:
            print(f.get_prop('path'))
            result = analysis.analysis(f.get_prop('path'), {})

            # 获取函数列表信息
            if 'procedures' in result:
                for procedure in result['procedures']:
                    new_node = data_service.create_node("Procedure", procedure['basic']['name'])
                    new_node.set_prop('name', procedure['basic']['name'])
                    new_node.set_prop('usr', procedure['basic']['usr'])
                    new_node.set_prop('type_str', procedure['basic']['type_str'])
                    new_node.set_prop('define', procedure['basic']['define'])
                    new_node.set_prop('start_line', procedure['basic']['start_line'])
                    new_node.set_prop('end_line', procedure['basic']['end_line'])
                    new_node.set_prop('start_col', procedure['basic']['start_col'])
                    new_node.set_prop('end_col', procedure['basic']['end_col'])

                    new_node.set_prop('params', json.dumps(procedure['params']))
                    new_node.set_prop('calls', json.dumps(procedure['calls']))

                    inputs = []
                    outputs = []

                    for var in procedure['vars']:
                        if var['io'] == 'in':
                            inputs.append(var)
                        else:
                            outputs.append(var)
                    
                    new_node.set_prop('inputs', json.dumps(inputs))
                    new_node.set_prop('outputs', json.dumps(outputs))

                    f.add_child(new_node)

                    


class DataExploreMenu:
    def __init__(self):
        self.menus = []
    def priorty(self):
        return 1000
    def add_menu(self, menu, nodes):
        self.menus = []
    
        kind = ""
        if len(nodes) == 1:
            node = nodes[0]
            kind = node.get_kind()
            if kind != "Project":
                return 

            action = menu.addAction("分析源码")

            obj = ActionNewNode(kind, node)
            self.menus.append(obj)
            action.triggered.connect(obj.on_analysis)

config = {
    'pluginid': 'UnitTest::UI::SourceAnalysis',
    "extensions" : [
        {
            "name": "PL::DataExplore::Menu",
            "id": "PL::DataExplore::Menu::UT::AnalysisSource",
            "impl": DataExploreMenu()
        }
    ]
}